package com.pi.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pi.base.common.BaseResult;
import com.pi.base.constant.SysStatics;
import com.pi.base.service.ISysUserService;
import com.pi.base.util.SysValidUtil;

@Controller
@RequestMapping("/login")
public class LoginController extends BaseController{
	
	private final Logger logger = LoggerFactory.getLogger(LoginController.class);
			
	@Resource
	private ISysUserService sysUserService;
	
	@RequestMapping(value="/checkLogin",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult checkLogin(@RequestParam(required=false) String userName,
			@RequestParam(required=false)String password,HttpServletRequest request, HttpServletResponse response) throws Exception {
	   String checkMess = SysValidUtil.validLogin(userName, password);
	   if(checkMess.length() > 0){
		   return BaseResult.newErrorResult(checkMess);
	   }
	   return sysUserService.checkLogin(userName, password, request, response);
	}

	/**
	  * 登录页面
	  * @param session
	  * @param model
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping("/goLogin")
	 public String goLogin(HttpServletRequest request){
	   String refer = request.getHeader("Referer");
		 logger.info("From-{},Url:{},--goLogin",refer,request.getRequestURI());
		 return "system/common/login";
	 }
	
	 @RequestMapping(value="/loginOut",method={RequestMethod.POST})
		@ResponseBody
	 public BaseResult loginOut(HttpSession httpSession) throws Exception{
		 if(httpSession.getAttribute(SysStatics.WEB_USER_SESSION) != null) {
			 httpSession.removeAttribute(SysStatics.WEB_USER_SESSION);
		 }
		 return BaseResult.newSuccessResult();
	 }
}
